import { ref, computed } from "vue";
import { useStore } from "vuex";

export default function useUpgradeApp() {
  const store = useStore();

  const isTipUpgrade = computed(() => store.getters.getIsTipUpgrade);

  // 更新提示框
  let upgradeShow = ref(false);
  // 下载进度条
  let isUpgrade = ref(false);
  // 下载进度
  let progressVal = ref(0);
  // 是否下载失败
  let isError = ref(false);

  // 只有在app上才会调用
  window.htmlPlus(() => {
    // 当前应用版本
    const version = window.plus.runtime.version;
    const newVersion = "20.0.0";

    // 是否需要更新
    let isNew = ref(true);

    // 判断是否是最新版本
    if (Number(version) < Number(newVersion)) {
      store.commit("PDATE_IS_TIPUPGRADE");
      console.log(isTipUpgrade.value, "))_)");
      isNew.value = true;
    }
    // 不是最新版本就提示更新
    if (!isNew.value) {
      upgradeShow.value = true;
    }
  });

  // 确定下载最新版本
  function toDownload() {
    console.log("点击了确定下载");
    const fileName = "faceSign.apk";
    const fileUrl =
      "https://dl.hdslb.com/mobile/latest/android64/iBiliPlayer-bili.apk?t=1654132384000";

    downloadFile(fileName, fileUrl);
  }

  // 下载文件
  function downloadFile(fileName, fileUrl) {
    console.log("下载开始了");
    window.plus.io.resolveLocalFileSystemURL(
      "_downloads/" + fileName,
      function (enter) {
        if (enter.isFile) {
          window.plus.nativeUI.toast("正在打开文件！");
          window.plus.runtime.openFile("_downloads/" + fileName);
          upgradeShow.value = false;
        }
      },
      function () {
        // 弹出下载进度条
        isUpgrade.value = true;
        var dtask = window.plus.downloader.createDownload(
          fileUrl,
          { filename: "_downloads/" + fileName },
          function (d, status) {
            // 下载完成
            if (status == 200) {
              // var newurl = window.plus.io.convertLocalFileSystemURL(d.filename);
              // window.plus.nativeUI.toast("下载成功" + newurl);
              window.plus.runtime.openFile("_downloads/" + fileName);
              upgradeShow.value = false;
            } else {
              isError.value = true;
              window.plus.nativeUI.toast("文件下载失败！");
              window.plus.downloader.clear();
            }
          }
        );

        dtask.addEventListener("statechanged", function (task, status) {
          if (!dtask) {
            return;
          }
          // console.log(task, "task");
          // console.log(task.state, "task.state");
          // console.log(status, "status");
          switch (task.state) {
            case 1:
              // window.plus.nativeUI.toast("开始下载...");
              break;
            case 2:
              // window.plus.nativeUI.toast("正在下载...");
              break;
            case 3: // 已接收到数据
              progressVal.value =
                (task.downloadedSize / task.totalSize) * 100 * 100;
              //psb1.progressbar({ progress: progressVal }).show();
              //dstatus[0].innerHTML = task.downloadedSize + '/' + task.totalSize;
              //hui.toast('下载进度：' + (task.downloadedSize + '/' + task.totalSize));
              console.log(status);
              // window.plus.nativeUI.toast(progressVal);
              break;
            case 4:
              dtask = null;
              upgradeShow.value = false;
              isUpgrade.value = false;
              progressVal.value = 0;
              break;
          }
        });
        dtask.start();
      }
    );
  }

  return { upgradeShow, isUpgrade, isError, progressVal, toDownload };
}
